﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;
using Libs.Configs;

namespace Libs.Content
{
    public class NewsSub
    {
        public int NewsSubID { get; set; }
        public int NewsID { get; set; }
        public string Title { get; set; }
        public string Lead { get; set; }
        public string SubLead { get; set; }
        public int NewspaperID { get; set; }
        public int UserID { get; set; }
        public DateTime CreatedTime { get; set; }
        public int ReturnValue { get; set; }

        public NewsSub()
        {

        }

        public NewsSub Get(int newsSubID)
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            return db.GetInstanceSP<NewsSub>("sp_NewsSub_Select", new SqlParameter("@NewsSubID", newsSubID));
        }

        public NewsSub Get()
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            return db.GetInstanceSP<NewsSub>("sp_NewsSub_Select", new SqlParameter("@NewsSubID", NewsSubID));
        }

        public List<NewsSub> GetList(int newsID)
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            return db.GetListSP<NewsSub>("sp_NewsSub_SelectList"
                , new SqlParameter("@NewsID", newsID));
        }

        public void Add()
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            SqlParameter[] pars = new SqlParameter[6];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@NewsID", NewsID);
            pars[2] = new SqlParameter("@Title", Title);
            pars[3] = new SqlParameter("@Lead", Lead);
            pars[4] = new SqlParameter("@NewspaperID", NewspaperID);
            pars[5] = new SqlParameter("@UserID", UserID);

            db.ExecuteNonQuerySP("sp_NewsSub_Insert", pars);
            NewsSubID = Convert.ToInt32(pars[0].Value);
        }

        public void Update()
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            SqlParameter[] pars = new SqlParameter[6];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@NewsSubID", NewsSubID);
            pars[2] = new SqlParameter("@NewsID", NewsID);
            pars[3] = new SqlParameter("@Title", Title);
            pars[4] = new SqlParameter("@Lead", Lead);
            pars[5] = new SqlParameter("@NewspaperID", NewspaperID);

            db.ExecuteNonQuerySP("sp_NewsSub_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        public void Delete(int newsSubID)
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            SqlParameter[] pars = new SqlParameter[2];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@NewsSubID", newsSubID);

            db.ExecuteNonQuerySP("sp_NewsSub_Delete", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        public void Delete()
        {
            DbHelper db = new DbHelper(Config.NewsConnectionStrings);
            SqlParameter[] pars = new SqlParameter[2];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@NewsSubID", NewsSubID);

            db.ExecuteNonQuerySP("sp_NewsSub_Delete", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

    }
}
